﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using ScriptLib = IWshRuntimeLibrary;

namespace StartSubMenu
{
    public partial class AddItemForm : Form
    {
        public string Title
        {
            get { return txtTitle.Text.Trim(); }
        }

        public string AppPath
        {
            get { return txtApp.Text.Trim(); }
        }

        public string Args
        {
            get { return txtArgs.Text.Trim(); }
        }

        private string _icon = null;
        public string IconPath
        {
            get { return _icon; }
        }

        //--------------------------------------------------------------------------------
        public AddItemForm()
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;
        }
        //--------------------------------------------------------------------------------
        public AddItemForm(string application, string arguments, string iconpath)
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;

            if(application.EndsWith(".lnk")) // use original icon path and title.
            {
                // set title from original file
                ScriptLib.WshShell shell = new ScriptLib.WshShell();
                ScriptLib.IWshShortcut shortcut = (ScriptLib.IWshShortcut)shell.CreateShortcut(application);
                txtTitle.Text = FileVersionInfo.GetVersionInfo(shortcut.TargetPath).FileDescription;

                txtApp.Text = shortcut.TargetPath;
                txtArgs.Text = shortcut.Arguments;

                if(iconpath == application) // if iconpath is the same as the link, point to the exe
                {
                    _icon = shortcut.TargetPath;
                }
            }
            else
            {
                txtTitle.Text = FileVersionInfo.GetVersionInfo(application).FileDescription;
                txtApp.Text = application;
                txtArgs.Text = arguments;
                _icon = iconpath;
            }

            if (_icon != "")
            {
                try
                {
                    picbox.Image = Icon.ExtractAssociatedIcon(_icon).ToBitmap();
                }
                catch { }
            }
        }
        //--------------------------------------------------------------------------------
        public AddItemForm(string title, string application, string arguments, string iconpath)
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;

            txtTitle.Text = FileVersionInfo.GetVersionInfo(application).FileDescription;
            txtApp.Text = application;
            txtArgs.Text = arguments;
            _icon = iconpath;

            if (_icon != "")
            {
                try
                {
                    picbox.Image = Icon.ExtractAssociatedIcon(_icon).ToBitmap();
                }
                catch { }
            }
        }
        //--------------------------------------------------------------------------------
        private void btnDone_Click(object sender, EventArgs e)
        {
            if (AppPath == "" || !File.Exists(AppPath))
            {
                MessageBox.Show("Application is missing or cannot be found");
                return;
            }

            if(Name == "")
            {
                MessageBox.Show("Missing item name");
                return;
            }
            
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        //--------------------------------------------------------------------------------
        private void btnAppBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Executable (*.exe)|*.exe|Batch (*.bat)|*.bat|ShortCut (*.lnk)|*.lnk";
            if (ofd.ShowDialog() != DialogResult.OK)
                return;

            txtApp.Text = ofd.FileName;

            if(_icon == null)
            {
                _icon = ofd.FileName;
                picbox.Image = Icon.ExtractAssociatedIcon(ofd.FileName).ToBitmap();
            }

            if(txtTitle.Text.Trim() == "")
            {
                txtTitle.Text = FileVersionInfo.GetVersionInfo(ofd.FileName).FileDescription;
            }
        }
        //--------------------------------------------------------------------------------
        private void btnIconBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Icon (*.ico)|*.ico|Executable (*.exe)|*.exe";
            if (ofd.ShowDialog() != DialogResult.OK)
                return;

            _icon = ofd.FileName;
            picbox.Image = Icon.ExtractAssociatedIcon(ofd.FileName).ToBitmap();
        }
        //--------------------------------------------------------------------------------
    }
}
